
import java.awt.Graphics;
import java.util.ArrayList;


public class TileHolder {
	
	private static final int NUM_LAND_TILES = 19;
	private static final int NUM_TILES = 37;
	private int size;
	private ArrayList<Tile> tiles;
	private ArrayList<Point> points;
	
	public TileHolder(int size, SOCApplet applet){
		tiles = new ArrayList<Tile>();
		points = new ArrayList<Point>();
		for(int i = 1; i <= NUM_TILES; i++){
			if (i <= NUM_LAND_TILES){
				Tile t = tileGenerator(tiles, i);
				//gives dieNum number to each tile, loops until success
				while(!t.dieNumGenerator(tiles)){}
				tiles.add(t);
			} else {
				Tile t = new Tile(i, 7, size);
				tiles.add(t);
			}	
		}
		for (int i = 0; i < tiles.size(); i++){
			for (int j = i; j < tiles.size(); j++){
				for (int k = j; k < tiles.size();k++){
					Point p = new Point(tiles.get(i), tiles.get(j
							), tiles.get(k), applet);
					if (p.exists()){
						points.add(p);
						PointDisplay pd = p.getPointDisplay();
					}
				}
			}
		}
		this.size = size;
	}
	
	public ArrayList<Tile> getTiles(){
		return tiles;
	}
	
	public void drawTiles(Graphics g){
		for(Tile t: tiles){
			t.drawSelf(size, g);
		}
		for (Point p: points){
			p.getPointDisplay().drawSelf(size, g);
		}
		
	}
	
	public ArrayList<Point> getPoints() {
		return points;
	}

	public void setPoints(ArrayList<Point> points) {
		this.points = points;
	}

	//finds appropriate random type for each new tile
	//see Tile.drawSelf() for what each number means
	public Tile tileGenerator(ArrayList<Tile> tiles, int i){
		int type = (int)(Math.random() * 6) + 1;
		int count = 0;
		for(Tile t: tiles){
			if(t.getType() == type)
				count++;
		}
		if(type < 4 && count < 4){
			return new Tile(i, type, size);
		} else if(type < 6 && count < 3){
			return new Tile(i, type, size);
		} else if(type == 6 && count == 0){
			return new Tile(i, 6, size);
		} else {
			return tileGenerator(tiles, i);
		}
	}
}
